﻿<%@ WebHandler Language="C#" Class="AutoUpdateHandler" %>

using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using Dln.Common;
using Dln.Common.Helper;
using Image = System.Drawing.Image;

public class AutoUpdateHandler : HandlerBase
{
    //login
    public void SetPwd()
    {
        string patientName = _httpRequestForm["PatientName"].ToString();
        string idNum = _httpRequestForm["IdNum"].ToString();
        string workStation = _httpRequestForm["WorkStation"].ToString();
        var url = CommonHelper.GetAppSettings("DlnService1");
        object[] args = new object[2];
        args[0] = (patientName);
        args[1] = (idNum);
        object result = WebServiceHelper.InvokeWebService(url, "Onlogin", args);
        if (result != null && (string)result != "False")
        {
            string message = result.ToString();
            object[] workBench = new object[1];
            workBench[0] = (message);

            object getWorkBenchResults = WebServiceHelper.InvokeWebService(url, "GetWorkBench", workBench);

            if (getWorkBenchResults != null)
            {
                DataSet ds = (DataSet)getWorkBenchResults;
                if (!ds.Tables[0].Columns.Contains("ResultDataSet"))
                {
                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        if (item["WSID"].ToString() == workStation)
                        {
                            //PrintSuccessJson(String.Format("PatientList.aspx?WSID={0}&Key={1}", workStation, message.Replace("+", "%2B")));
                            PrintSuccessJson(String.Format("TestList.aspx?WSID={0}&Key={1}", workStation, message.Replace("+", "%2B")));
                        }
                    }

                }
            }
        }
        else
        {
            PrintErrorJson("输入的用户名或密码有误");
        }
    }

    //患者列表
    public void PatientList()
    {
        string wsid = HttpContext.Current.Request.Form["WSID"];
        string key = HttpContext.Current.Request.Form["KEY"];
        if (!string.IsNullOrEmpty(wsid) && !string.IsNullOrEmpty(key))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[2];
            args[0] = (key);
            args[1] = (wsid);
            object result = WebServiceHelper.InvokeWebService(url, "GetInpatientInfo_Only", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

    /// <summary>
    /// 获取工作台
    /// </summary>
    public void GetWorkStation()
    {
        string patientName = _httpRequestForm["PatientName"].ToString();
        string idNum = _httpRequestForm["IdNum"].ToString();
        var url = CommonHelper.GetAppSettings("DlnService1");
        object[] args = new object[2];
        args[0] = (patientName);
        args[1] = (idNum);
        object result = WebServiceHelper.InvokeWebService(url, "Onlogin", args);
        if (result != null && (string)result != "False")
        {
            string message = result.ToString();
            object[] workBench = new object[1];
            workBench[0] = (message);

            object getWorkBenchResults = WebServiceHelper.InvokeWebService(url, "GetWorkBench", workBench);

            if (getWorkBenchResults != null)
            {
                DataSet ds = (DataSet)getWorkBenchResults;
                if (!ds.Tables[0].Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(ds.Tables[0]));
                    HttpContext.Current.Response.End();
                }
            }
        }
        HttpContext.Current.Response.Write("");
        HttpContext.Current.Response.End();
    }

    /// <summary>
    /// 扫描二维码
    /// </summary>
    public void FileImg()
    {
        try
        {
            this.Context.Response.ContentType = "text/plain";

            if (Context.Request.Files.Count > 0)
            {
                //保存image图片
                var file = Context.Request.Files[0];
                var filename = Guid.NewGuid().ToString() + ".jpg";
                const string filepath = "D:/DlnTest/Images/";
                if (!Directory.Exists(filepath))
                {
                    Directory.CreateDirectory(filepath);
                }
                file.SaveAs(filepath + filename);
                //请求webservice
                object[] args = new object[1];
                args[0] = "D:\\DlnTest\\Images\\" + filename;
                var url = CommonHelper.GetAppSettings("DlnService1");
                object getWorkBenchResults = WebServiceHelper.InvokeWebService(url, "GetQrCodeForPath", args);
                File.Delete("D:\\DlnTest\\Images\\" + filename);  //删除服务器图片

                if (getWorkBenchResults != null)
                {
                    var sb = getWorkBenchResults.ToString();
                    this.Context.Response.Write("{\"status\":\"" + 1 + "\",\"info\":\"" + sb + "\"}");
                }
                else
                {
                    this.Context.Response.Write("{\"status\":\"" + 0 + "\",\"info\":\"" + "二维码解析失败" + "\"}");
                }

            }
        }
        catch (Exception e)
        {
            this.Context.Response.Write("{\"status\":\"" + 0 + "\",\"info\":\"" + "二维码解析失败1" + e.Message + "\"}");
        }
    }

    /// <summary>
    /// 获取科室病区
    /// </summary>
    public void GetCurrInHosp_ByDiag()
    {
        string key = HttpContext.Current.Request.Form["KEY"];
        if (!string.IsNullOrEmpty(key))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[1];
            args[0] = (key);
            object result = WebServiceHelper.InvokeWebService(url, "GetCurrInHosp_ByDiag", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

    /// <summary>
    /// 昨日入院明细列表
    /// </summary>
    public void GetCurrDayInHospNum_Item()
    {
        string key = HttpContext.Current.Request.Form["KEY"];
        if (!string.IsNullOrEmpty(key))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[1];
            args[0] = (key);
            object result = WebServiceHelper.InvokeWebService(url, "GetCurrDayInHospNum_Item", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

    /// <summary>
    /// 昨日出院明细列表
    /// </summary>
    public void GetCurrOutHospNum_Item()
    {
        string key = HttpContext.Current.Request.Form["KEY"];
        if (!string.IsNullOrEmpty(key))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[1];
            args[0] = (key);
            object result = WebServiceHelper.InvokeWebService(url, "GetCurrOutHospNum_Item", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

    /// <summary>
    /// 昨日退院患者列表
    /// </summary>
    public void GetCurrLeaveHospNum_Item()
    {
        string key = HttpContext.Current.Request.Form["KEY"];
        if (!string.IsNullOrEmpty(key))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[1];
            args[0] = (key);
            object result = WebServiceHelper.InvokeWebService(url, "GetCurrLeaveHospNum_Item", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

    /// <summary>
    /// 今日在院患者列表
    /// </summary>
    public void GetCurrInHosp_ByDiag_Item()
    {
        string key = HttpContext.Current.Request.Form["KEY"];
        string diagOrgId = HttpContext.Current.Request.Form["DiagOrgId"];
        if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(diagOrgId))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[2];
            args[0] = (key);
            args[1] = (diagOrgId);
            object result = WebServiceHelper.InvokeWebService(url, "GetCurrInHosp_ByDiag_Item", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

    /// <summary>
    /// 昨日危重患者
    /// </summary>
    public void GetCurrCriticallyIllInHospNum_Item()
    {
        string key = HttpContext.Current.Request.Form["KEY"];
        if (!string.IsNullOrEmpty(key))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[1];
            args[0] = (key);
            object result = WebServiceHelper.InvokeWebService(url, "GetCurrCriticallyIllInHospNum_Item", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

    /// <summary>
    /// 科室收入列表
    /// </summary>
    public void GetYesterdayAmount_ByOrg()
    {
        string key = HttpContext.Current.Request.Form["KEY"];
        string SearchTime = HttpContext.Current.Request.Form["SearchTime"];
        if (!string.IsNullOrEmpty(key))
        {
            var url = CommonHelper.GetAppSettings("DlnService1");
            object[] args = new object[2];
            args[0] = (key);
            args[1] = (SearchTime);
            object result = WebServiceHelper.InvokeWebService(url, "GetYesterdayAmount_ByOrg", args);
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(dc));
                    HttpContext.Current.Response.End();
                }

            }
        }
    }

}